<?php

namespace App\Http\Controllers\Api\SmallApp\User;

use App\Http\Controllers\Common\ApiController;
use App\Models\Constant\UserConstant;
use App\Models\Repair\RepairFactoryAdmin;
use App\Models\User\ChengBao;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;

class RegisterController extends ApiController {

    public function register(Request $request) {

        $phone = $request->input('phone');
        $verify_code = $request->input('verify_code');
        $password = $request->input('password');
        $car_jia_number = '';
        $car_number = $request->input('car_number');
        $car_number = Str::replace(' ', '', $car_number);

        //新能源&教练车=》豫A12345D学（13位），为了保险起见，选择14位为临界点
        if(strlen($car_number)>=14){
            $car_jia_number = $car_number;
            $car_number = '';
        }


        //$id_card = $request->input('id_card');
        //$password_confirmation = $request->input('password_confirmation');
        //$baodan_order_number = $request->input('baodan_order_number');

        $this->validate($request, [
            'password'=>'required|confirmed|between:8,16|regex:/^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S+$/',
            'phone'=>'required|unique:user,phone',
            'verify_code'=>'required|Integer',
            /*'id_card'=>['required',
                'regex:/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/'],*/
            'car_number'=>'required',
            //'baodan_order_number'=>'required',
        ],[],[
            'car_number'=>'车牌号/车架号',
            //'baodan_order_number'=>'保险单号',
        ]);

        if(env('APP_ENV')!='local'){
            $key = "verifyCode:$phone:$verify_code";
            if(Redis::exists($key)!=1){
                return responseFail('验证码错误！');
            }
        }

        try {
            if(!empty($car_number)){
                $chengBao = ChengBao::where(compact('car_number'))->first();
            }
            if(!empty($car_jia_number)){
                $chengBao = ChengBao::where(compact('car_jia_number'))->first();
            }
            if(empty($chengBao)){
                return responseFail('很遗憾，未查到您的投保信息！');
            }
            $password = md5($password);
            $type = UserConstant::TYPE_NORMAL;
            if(RepairFactoryAdmin::where(compact('phone'))->exists()){
                $type = UserConstant::TYPE_ALL;
            }

            $call_phone = $phone;
            //$info = compact('phone','id_card','car_number','baodan_order_number','password','type','call_phone');
            $info = compact('phone','car_number','password','type','call_phone','car_jia_number');
            $user = User::create($info);

            $user->token = createNewToken($user->id);

            return responseSuccess($user->toArray());
        }catch (\Exception $e){
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }

    }

    public function getVerifyCode(Request $request) {
        $phone = $request->input('phone');
        $this->validate($request, [
            'phone'=>'required|regex:/^1[3456789]\d{9}$/',
        ]);
        if(env('APP_ENV','local')=='local'){
            $verifyCode = rand(1000,9999);
            return responseSuccess(compact('verifyCode'));
        }else{
            sendVerifyCode($phone);
            return responseSuccess();
        }
    }



}
